﻿
namespace HSS.Xaml.UI.Interactivity
{
	#region DataGridDoubleClickTrigger
	/// <summary>
	/// DoubleClickTrigger for a DataGrid.
	/// </summary>
	public class DataGridDoubleClickTrigger : DoubleClickTrigger<System.Windows.Controls.DataGrid>
	{
		#region Constructors
		/// <summary>
		/// Constructor
		/// </summary>
		public DataGridDoubleClickTrigger()
		{
		}
		#endregion

		#region Overrides
		/// <summary>
		/// Connect to the AssociatedObject events that will trigger the DoubleClick.
		/// </summary>
		protected override void ConnectEvent()
		{
			this.AssociatedObject.LoadingRow += OnLoadingRow;
			this.AssociatedObject.UnloadingRow += OnUnloadingRow;
		}
		/// <summary>
		/// Disconnect from the AssociatedObject events that will trigger the DoubleClick.
		/// </summary>
		protected override void DisconnectEvent()
		{
			this.AssociatedObject.LoadingRow -= OnLoadingRow;
			this.AssociatedObject.UnloadingRow -= OnUnloadingRow;
		}
		#endregion

		#region Event Handlers
		void OnLoadingRow(object sender, System.Windows.Controls.DataGridRowEventArgs e)
		{
			e.Row.MouseLeftButtonUp += new System.Windows.Input.MouseButtonEventHandler(Row_MouseLeftButtonUp);
		}
		void OnUnloadingRow(object sender, System.Windows.Controls.DataGridRowEventArgs e)
		{
			e.Row.MouseLeftButtonUp -= new System.Windows.Input.MouseButtonEventHandler(Row_MouseLeftButtonUp);
		}
		void Row_MouseLeftButtonUp(object sender, System.Windows.Input.MouseButtonEventArgs e)
		{
			if (base.DoubleClickDetection.ProcessClick(sender))
				base.OnDoubleClick();
		}
		#endregion
	}
	#endregion
}